<!-- 物料管理 -->
<template>
  <!-- <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
          <div class="search-btn search-btns" @click.stop="searchPage()">
            <i class="el-icon-circle-check"></i>
            <p>查询</p>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">物料名称</div>
            <el-input v-model="name" clearable @change="searchPage" />
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <ActionBar :add-active="addActive" :del-active="delActive" :page-no="163" @add="add" @del="del" />
    </template>
    <template v-slot:content>

      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border
        height="calc(100% - 50px)" :row-key="row => { return row.materialId; }
        " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.materialId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="name" label="物料名称" />
        <el-table-column prop="materialTypeName" label="物料类型名称" />
        <el-table-column prop="brand" label="品牌" />
        <el-table-column label="物料规格">
          <template v-if="scoped.row.materialId" slot-scope="scoped">
            {{ `
            ${scoped.row.ratio ? scoped.row.ratio : ''}
            ${scoped.row.basicUnitName ? scoped.row.basicUnitName : ''}
            ${scoped.row.auxiliaryUnitName && scoped.row.auxiliaryUnitName !== " " ? '/' : ''}
            ${scoped.row.auxiliaryUnitName ? scoped.row.auxiliaryUnitName : ''}
            `}}
          </template>
        </el-table-column>
        <el-table-column prop="materialType" label="物料型号" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" width="250">
          <template v-if="scoped.row.materialId" slot-scope="scoped">
            <div class="table-btns-box">
              <div class="table-btns edit-btns" @click="edit(scoped.row)">
                <p>修改</p>
              </div>
              <div class="table-btns del-btns" @click="delOne(scoped.row)">
                <p>删除</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
        @changeCurrentPage="changeCurrentPage" />
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </BaseMainSlot> -->


  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" @search="search">
      </SearchForm>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" stripe border :row-key="row => { return row.materialId; }
        " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
        <el-table-column type="selection" width="55" :reserve-selection="true"
          :selectable="(row) => { return !row.selectable }" />
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scoped">{{ scoped.row.materialId ? scoped.$index + 1 : '' }}</template>
        </el-table-column>
        <el-table-column prop="name" label="物料名称" width="400"/>
        <el-table-column prop="materialTypeName" label="物料类型名称" />
        <el-table-column label="物料规格">
          <template v-if="scoped.row.materialId" slot-scope="scoped">
            {{ `
            ${scoped.row.ratio ? scoped.row.ratio : ''}
            ${scoped.row.basicUnitName ? scoped.row.basicUnitName : ''}
            ${scoped.row.auxiliaryUnitName && scoped.row.auxiliaryUnitName !== " " ? '/' : ''}
            ${scoped.row.auxiliaryUnitName ? scoped.row.auxiliaryUnitName : ''}
            `}}
          </template>
        </el-table-column>
        <el-table-column prop="brand" label="品牌" >
          <template slot-scope="scoped">{{ scoped.row.brand|| '--' }}</template>
        </el-table-column>
        <el-table-column prop="materialType" label="物料型号" >
          <template slot-scope="scoped">{{ scoped.row.materialType|| '--' }}</template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" >
          <template slot-scope="scoped">{{ scoped.row.remark|| '--' }}</template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template v-if="scoped.row.materialId" slot-scope="scoped">
            <el-button type="primary" :disabled="scoped.row.ncPkMaterial" plain @click="edit(scoped.row)">修改</el-button>
            <el-button type="danger" plain @click="delOne(scoped.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="changeCurrentPage" :size="pageSize" :page="currentPage" :total="totalSize">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </MainSlot>
</template>

<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from '@/components-production-management/Pagination';
import SearchForm from '@/components-production-management/SearchForm';
import BtnSlot from '@/components-production-management/BtnSlot';

import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import MaterialApi from "@/api/basic-info-manage/material";
export default {
  name: "MaterialManage",
  components: { BtnSlot, SearchForm, Pagination, MainSlot, AddEditDialog },
  mixins: [mymixin],
  data() {
    return {
      // 查询条件：名称
      name: "",
      checkedIdList: [],
      btnList: ['add', 'delete'],
      // 主单位
      mainunit: "",
      formItemList: [
        {
          label: '物料名称',
          type: 'input',
          param: 'name',
          multiple: false,
          isShow: true
        }
      ]
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    this.queryPageList();
  },
  mounted() {

  },
  methods: {
    search(data) {
      this.currentPage = 1;
      this.name = data.name;
      this.queryPageList();
    },
    // 查询
    queryPageList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          name: this.name
        }
      };
      MaterialApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.materialId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.materialId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    importData() {
      console.log('导入')
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.materialId) {
          return item.materialId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 表格删除
    delmessage() {
      MaterialApi.delListById({ idList: this.checkedIdList }).then(res => {
        if (res.success) {
          this.$message.success("删除成功");
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },

    //获取选中Btn
    getBtnEvent(flag) {
      if (flag == 'add') {
        this.add();
      } else if (flag == 'delete') {
        this.delmessage()
      }
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",

      })
        .then(() => {
          MaterialApi.delById({ id: data.materialId }).then(res => {
            if (res.success === true) {
              this.$message.success("删除成功!");
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.resetActionBar();
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 表格删除
    delmessage() {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",

      })
        .then(() => {
          MaterialApi.delListById({ idList: this.checkedIdList }).then(res => {
            if (res.success) {
              this.$message.success("删除成功");
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.resetActionBar();
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });

    },
    handlerNameChange() {
      this.currentPage = 1
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.materialId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
